<%@ page import="java.util.jar.Attributes.Name"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="support.*, java.util.*" %>
    
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Degree-Location</title>
<link href="main.css" rel="stylesheet" type="text/css">
</head>
<body>

<%
	Applicant a = (Applicant) session.getAttribute("applicant");
	if(a.getAddress() == null)
	{
		String street = request.getParameter("street");
		String city = request.getParameter("city");
		String state = request.getParameter("state");
		if (state==null) state = "";
		String zipcode = request.getParameter("zipcode");
		String countrycode = request.getParameter("countrycode");
		if (countrycode==null) countrycode = "";
		String areacode = request.getParameter("areacode");
		String phone = request.getParameter("phone");
		Address address = new Address();
		address.setAreacode(areacode);
		address.setCity(city);
		address.setCountrycode(countrycode);
		address.setPhone(phone);
		address.setState(state);
		address.setStreet(street);
		address.setZipcode(zipcode);
		a.setAddress(address);
	}
	DatabaseAccessObject dao = new DatabaseAccessObject();
	//locations from the database
	LinkedList<Location> locations =  dao.getLocations();
	LinkedList<Degree> degrees = a.getDegrees();
	Degree degree = null;
	degree = new Degree();
	session.setAttribute("degree", degree);
	// degrees.add(degree);
	/* Instead of adding degree object directly here, we save it in a session first. In next 
	page (degree_university.jsp), we will add the degree object to applicant then delete 
	the session. This design is to guarentee that the degree object will be only be added
	once. Even though the user try to reload the page, the object will not be added duplicately */
	
	Location loc = null;
	int id;
	/*if user entered any location that doesn't exist in the database,
	retrieve the distinct names of user entered locations from the session,
	and show them after the locations from the databases
	*/
	if(degrees != null)
	{
		Set<String> location_input_set = new HashSet<String>();
		for(int i=0;i<degrees.size();i++)
		{
			loc= degrees.get(i).getUniversity().getLocation();
			if (loc.getId() == -1)
			{
				id = dao.getLocationIdByName(loc.getName());
				if(id == -1)
				{
					if(!location_input_set.contains(loc.getName()))
					{
						location_input_set.add(loc.getName());
						locations.add(loc);
					}
				}
			}
		}
	}
%>

<h1>Provide degrees - Choose location</h1>
	<table class="multicolumn">
		<tr>
			<% 
			int cols = 3;
			int itemsPerCol = locations.size() / cols;
			if (locations.size() % cols != 0) itemsPerCol += 1;
			for(int i=0; i<locations.size(); i++){
				if (i % itemsPerCol == 0)  out.println("<td>");
				String location_name = locations.get(i).getName();
				out.println("<a class=\"list\" href=\"degree_university.jsp?location_id=" + locations.get(i).getId() +  "&location_name=" + location_name +  "\">" + location_name + "</a>");
				if (i % itemsPerCol == itemsPerCol-1 || i==locations.size() -1 )  out.println("</td>");
			};
			%>
		</tr>
	</table>
	<hr>
	<form name="form1" action="degree_university.jsp" method="get">
		<h2>If not found, input your degree location below:</h2>
		<input type="text" name="location" value="">
		<input type="submit" value = "submit">
	</form>	
</body>
</html>